package com.fmc123.base.modules.controller;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fmc123.base.modules.entity.SysUser;
import com.fmc123.base.modules.service.UserService;
import com.fmc123.base.pesistence.Page;
import com.fmc123.base.utils.StringUtils;
import com.fmc123.base.web.BaseController;
import com.fmc123.base.web.DWZRender;
import com.fmc123.base.web.Render;

@Controller
@RequestMapping("/management/user")
public class UserController extends BaseController {
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private UserService userService;
	
	/**
	 * 此方法会在这个控制器中的所有方法执行前被调用
	 * 会自动往Model中增加一个sysUser属性
	 * @param id
	 * @return
	 */
	@ModelAttribute("sysUser")
	public SysUser get(@RequestParam(required = false) String id){
	  if(StringUtils.isPresent(id)){
	     return userService.get(id);
	  }else{
	    return new SysUser();
	  }
	}
	
	@RequestMapping(value={"/list",""})
	public String index(Model model,SysUser user){
	  Page<SysUser> datas=userService.find(initPage(SysUser.class), user);
	  model.addAttribute("datas", datas);
	  model.addAttribute("user", user);
	  return "modules/sys/userList";
	}
	
	@RequestMapping(value={"/edit"})
	public String edit(SysUser user,Model model){
	  model.addAttribute("user", user);
	  return "modules/sys/userForm"; 
	}
 
	@RequestMapping(value={"/save"})
    public void save(Model model ,SysUser user){
	  userService.update(user);
	  tojsonOk(new DWZRender());
    }
	
	@RequestMapping(value={"/delete"})
    public void delete(SysUser user){
	  userService.delete(user);
	  tojsonOk(new DWZRender());
    }
}
